package com.syh.jn.scm.domain.supplier.entity.base;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.spring.SpringContextUtils;
import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.jn.scm.domain.supplier.repository.base.ProjectReadRepository;
import com.syh.jn.scm.domain.support.SuperBusinessAggregate;
import com.syh.jn.scm.dto.supplier.base.project.ProjectMto;
import com.syh.jn.scm.dto.supplier.base.project.ProjectSto;
import com.syh.jn.scm.exception.BusinessException;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 运营项目
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class Project extends SuperBusinessAggregate<Long> {
  /**
   * 创建运营项目
   *
   * @param sto
   * @return
   */
  public void create(ProjectSto sto) {
    //初始化运营项目
    ExtBeanUtils.copyProperties(sto, this);
    this.status = 1;
    initDefault();
  }

  /**
   * 修改运营项目
   *
   * @param mto
   * @return
   */
  public void modify(ProjectMto mto) {
    ExtBeanUtils.copyProperties(mto, this);
    this.lastUpdateTimeStamp = System.currentTimeMillis();
  }

  /**
   * 删除运营项目
   */
  public void destroy() {
    ProjectReadRepository projectReadRepository = (ProjectReadRepository) SpringContextUtils.getBean("projectRepositoryImpl");
    Wrapper we = Wrapper.build();
    we.append(Restrictions.rightLike("catalogCode", this.catalogCode + "-"));
    List<Project> projectList = projectReadRepository.getList(we);
    if (projectList.size() > 0 && projectList != null) {
      throw new BusinessException("请先删除子项目");
    }
  }

  /**
   * 启用
   */
  public void enabled() {
    this.status = 1;
    List<String> catalogIds = new ArrayList<>(Arrays.asList(this.catalogCode.split("-")));
    if (catalogIds.size() > 1)
      catalogIds.remove(catalogIds.size() - 1);

    Wrapper we = Wrapper.build();
    we.append(Restrictions.in("id", catalogIds.toArray()));
    ProjectReadRepository projectRepository = (ProjectReadRepository) SpringContextUtils.getBean(ProjectReadRepository.class);
    this.parents = projectRepository.getList(we);
    this.parents.forEach(parent -> {
      parent.setStatus(this.status);
    });
  }

  /**
   * 停用
   */
  public void disabled() {
    this.status = -1;

    Wrapper we = Wrapper.build();
    we.append(Restrictions.rightLike("catalogCode", this.catalogCode + "-"));
    ProjectReadRepository projectRepository = (ProjectReadRepository) SpringContextUtils.getBean(ProjectReadRepository.class);
    this.subProjects = projectRepository.getList(we);
    this.subProjects.forEach(subProject -> {
      subProject.setStatus(this.status);
    });
  }

  /**
   * 状态（-1：停用 1：启用）
   */
  private Short status;
  /**
   * 上级项目id
   */
  private Long supId;
  /**
   * 项目编码
   */
  private String code;
  /**
   * 项目名称
   */
  private String name;
  /**
   * 排序等级
   */
  private Integer orderLevel;
  /**
   * 目录树    格式：1-11-12  1：父父类id，11：父类id，12：当前id
   */
  private String catalogCode;
  /**
   * 父项目
   */
  private List<Project> parents;
  /**
   * 子项目
   */
  private List<Project> subProjects;
  /**
   * items
   */
  private List<ProjectItem> items;
  /**
   * 描述
   */
  private String description;
}
